import global from './global.js'
import {
    toast
} from './util.js'
import {
    getUserOpenId,
    getUserInfo
} from '../api/app.js'
class User {
    constructor() {
        this.init()
    }
    init() {
        this.alipayAccount = ''
        this.openid = ''
        this.session_key = ''
        this.userId = ''
        this.mpOpenid = ''
        this.userName = ''
        this.phoneNum = ''
        this.cardNo = ''
        this.avatarUrl = ''
        this.hasCardPhoto = false
        this.waiting = true
        this.isAttention = false
        this.isOpenSalaryCard = false
        this.ownIdCardBack = ''
        this.ownIdCardFront = ''
        this.isNeedAuthSalaryCard = false // 查看是否强制开通微工卡
        this.bankCardNo = ''
        this.bankCardNoNetwork = '' // 网商银行
        this.sex = ''
        this.openBankBranch = ''
        this.openBankCity = ''
        this.openBankName = ''
        this.bankCardImg = ''
        this.hasOpenBankInfo = false
        this.hasOpenBankCity = false
        this.alipayOpenId = ''
    }
    reLogin() {
        return new Promise((reslove, reject) => {
            const t = this
            wx.login({
                success(res) {
                    console.log('获取到用户code:' + res.code)
                    if (res.code) {
                        // 发起网络请求
                        getUserOpenId({
                            appId: global.appId,
                            code: res.code
                        }).then((res) => {
                            t.session_key = res.sessionKey
                            t.openid = res.openid
                            const token = res.token
                            global.token = token
                            if (!res.token) {
                                t.waiting = false
                                return
                            }
                            getUserInfo({
                                openId: res.openid
                            }).then(res => {
                                console.log(res, '信息')
                                t.waiting = false
                                if (res) {
                                    t.alipayAccount = res.alipayAccount?res.alipayAccount:''
                                    t.userId = res.userId
                                    t.mpOpenid = res.mpOpenid
                                    t.phoneNum = res.phone
                                    t.cardNo = res.idCardNo
                                    t.userName = res.userName
                                    t.avatarUrl = res.userHeadUrl
                                    t.hasCardPhoto = (!!res.ownIdCardFront && !!res.ownIdCardBack)
                                    t.isAttention = res.isAttention
                                    t.ownIdCardBack = res.ownIdCardBack
                                    t.ownIdCardFront = res.ownIdCardFront
                                    t.isOpenSalaryCard = res.isOpenSalaryCard
                                    t.bankCardNo = res.bankCardNo
                                    t.bankCardNoNetwork = res.bankCardNoNetwork
                                    t.sex = res.sex
                                    t.bankCardImg = res.bankCardImg
                                    t.openBankBranch = res.openBankBranch
                                    t.openBankCity = res.openBankCity
                                    t.openBankName = res.openBankName
                                    t.hasOpenBankInfo = !!res.openBankName
                                    t.hasOpenBankCity = !!res.openBankCity
                                    t.alipayOpenId = res.alipayOpenId
                                    reslove(t)
                                } else {
                                    reject()
                                }
                            })
                        })

                    }
                },
                fail() {
                    t.waiting = false
                    toast('获取用户信息失败', 'error')
                    reject()
                }
            })
        })
    }
    // 登录
    login() {
        // 如果已登录（使用Promise.relove()需要开增强编译）
        if (this.userId) {
            this.waiting = false
            return new Promise.reslove(this)
        }
        // 未登录
        this.reLogin()
    }
    // 等待登录
    waitLogin() {
        return new Promise((reslove, reject) => {
            const wait = setInterval(() => {
                if (!this.waiting) {
                    clearInterval(wait)
                    if (this.userId) {
                        reslove(this)
                    } else {
                        reject()
                    }
                }
            }, 500)
        })
    }
    exit() {
        this.userId = ''
        this.phoneNum = ''
        this.cardNo = ''
        this.userName = ''
        this.avatarUrl = ''
        this.hasCardPhoto = ''
        this.isOpenSalaryCard = false
        global.token = ''
        wx.switchTab({
            url: '/pages/index/index',
        })
    }
    clear() {
        this.userId = ''
        this.phoneNum = ''
        this.cardNo = ''
        this.userName = ''
        this.avatarUrl = ''
        this.hasCardPhoto = ''
        this.isOpenSalaryCard = false
        global.token = ''
    }
    /**
     * 前往注册
     * @param {boolean} isRedirect 是否重定向跳转（不保留当前页面）
     * @param {string} backUrl 注册完成后返回的页面
     */
    goRegister(isRedirect = false, backUrl = '') {
        global.registerBack = backUrl
        const url = '/pages/login/login'
        if (isRedirect) {
            wx.redirectTo({
                url
            })
        } else {
            wx.navigateTo({
                url
            })
        }
    }
}
const user = new User()
export default user